#pragma once
#include "protocol.hpp"


class calculate
{
public:
    response execute(const request& req)
    {
        response resp; // code: 0表示成功
        switch (req.get_oper())
        {
        case '+':
            resp.set_result(req.get_x() + req.get_y());
            break;

        case '-':
            resp.set_result(req.get_x() - req.get_y());
            break;

        case '*':
            resp.set_result(req.get_x() * req.get_y());
            break;

        case '/':
            {
                if (req.get_y() == 0)
                    resp.set_code(DIVISION_ZERO_ERR); 
                else
                    resp.set_result(req.get_x() / req.get_y());
            }
            break;

        case '%':
            {
                if (req.get_y() == 0)
                    resp.set_code(MODULO_ZERO_ERR);
                else
                    resp.set_result(req.get_x() % req.get_y());
            }
            break;

        default:
            resp.set_code(OPERATOR_UNKNOWN);
            break;
        }

        return resp;
    }
};